//
//  ST_TropNormalHeaderView.swift
//  btvideo
//
//  Created by mac on 2021/7/9.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit

class ST_TropNormalHeaderView: UICollectionReusableView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        [champion_view,runner_up_view,third_place_view].forEach({ addSubview($0) })
        
        champion_view.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.centerX.equalToSuperview()
            make.width.equalTo(TIANCompatibleValue(value: 84))
            make.bottom.equalToSuperview()
        }
        
        runner_up_view.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(22)
            make.left.equalToSuperview().offset(TIANCompatibleValue(value: 38))
            make.width.equalTo(TIANCompatibleValue(value: 84))
            make.bottom.equalToSuperview()
        }
        
        third_place_view.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(22)
            make.right.equalToSuperview().offset(TIANCompatibleValue(value: -38))
            make.width.equalTo(TIANCompatibleValue(value: 84))
            make.bottom.equalToSuperview()
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    fileprivate lazy var champion_view:ST_WinnerView = {
        let control = ST_WinnerView(cupSize: CGSize(width: 78, height: 110))
        control.alpha = 0
        return control
    }()
    
    fileprivate lazy var runner_up_view:ST_WinnerView = {
        let control = ST_WinnerView(cupSize: CGSize(width: 66, height: 97))
        control.alpha = 0
        return control
    }()
    
    fileprivate lazy var third_place_view:ST_WinnerView = {
        let control = ST_WinnerView(cupSize: CGSize(width: 66, height: 97))
        control.alpha = 0
        return control
    }()
    
}

extension ST_TropNormalHeaderView{
    
    func bind(data_array:[ST_TrophyNormalCellData]) {
        
        
        
        champion_view.bind(data: data_array.first, hat: ST_ImageUtils.imageName("rank_champion_icon"))
        runner_up_view.bind(data: data_array[safe:1], hat: ST_ImageUtils.imageName("rank_runner_up_icon"))
        third_place_view.bind(data: data_array[safe:2], hat: ST_ImageUtils.imageName("rank_third_place_icon"))
        
    }
    
}


//MARK: - winner View
fileprivate class ST_WinnerView:UIView{
    
    convenience init(cupSize:CGSize){
        self.init(frame: .zero)
        
        hat_image_view.snp.makeConstraints { (make) in
            make.centerX.top.equalToSuperview()
            make.size.equalTo(cupSize)
        }
        
        avatar_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(cupSize.width)
            make.bottom.centerX.equalTo(hat_image_view)
        }
        
        avatar_image_view.cornerRadius = cupSize.width/2.0
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        [avatar_image_view,hat_image_view,titleLabel,content_label].forEach{ addSubview($0) }
        
        content_label.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-20)
            make.centerX.equalToSuperview()
            make.height.equalTo(17)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-38)
            make.centerX.equalToSuperview()
            make.width.lessThanOrEqualTo(80)
            make.height.equalTo(20)
        }
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var avatar_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        let tap = UITapGestureRecognizer(target: self, action: #selector(click(gez:)))
        control.isUserInteractionEnabled = true
        control.addGestureRecognizer(tap)
        return control
    }()
    
    fileprivate lazy var hat_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        return control
    }()
    
    
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = .white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        return control
    }()
    
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0.6047196062)
        control.font = UIFont.systemFont(ofSize: 12)
        return control
    }()
    
    weak var data:ST_TrophyNormalCellData?
    
    func bind(data:ST_TrophyNormalCellData?,hat:UIImage?)  {
        
        self.data = data
        
        guard let item = data else {
            self.alpha = 0
            return
        }
        
        self.alpha = 1
        
        avatar_image_view.pin_setImage(from: item.user.avatar.compress_small_image(width: 48*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        titleLabel.text = item.user.nickname
        
        content_label.text = item.currency.string
        
        hat_image_view.image = hat
    }
    
    @objc fileprivate func click(gez:UITapGestureRecognizer){
        
        guard let d =  data else { return  }
        
        if d.user.rankClose == 2{
            self.parentViewController?.toast(text: "对方匿名，不能查看个人主页")
            return
        }
        
        
        let vc = ST_PairsCenterViewController()
        
        vc.accid = d.user.accid
        
        let nav = PairsCenterInteractiveNavigationController.init(rootViewController: vc)
        
        nav.overCurrentContext = true
        
        GlobalPresent(toController: nav)
        
        
    }
}

